package com.qaml.mes.device.domain.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qaml.mes.device.api.CheckUpkeepUserService;
import com.qaml.mes.device.dao.CheckUpkeepUserDO;
import com.qaml.mes.device.enums.DeletedEnum;
import com.qaml.mes.device.repository.CheckUpkeepUserRepository;
import com.qaml.mes.device.domain.convert.CheckUpkeepUserConvert;
import com.qaml.mes.device.enums.CheckUpkeepUserTypeEnum;
import com.qaml.mes.device.vo.eqp.CheckUpkeepUserVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 检保关联人员表 服务实现类
 * </p>
 *
 * @author leikai
 * @since 2024-02-27
 */
@Service
public class CheckUpkeepUserServiceImpl implements CheckUpkeepUserService {

    @Resource
    private CheckUpkeepUserRepository checkUpkeepUserRepository;


    /**
     * 删除检保关联人员信息
     *
     * @param checkUpkeepId 检保信息编号
     */
    @Override
    public void deleteByCheckUpkeepId(String checkUpkeepId) {

        // 1,获取检保关联人员信息
        List<CheckUpkeepUserDO> checkUpkeepUserDOS = checkUpkeepUserRepository.getByCheckUpkeepId(checkUpkeepId);

        // 获取所有关联人员的Ids
        List<String> ids = checkUpkeepUserDOS.stream().map(CheckUpkeepUserDO::getId).collect(Collectors.toList());
        // 批量删除
        // 批量修改,设置isDeleted (在Mapper中完成)
        checkUpkeepUserRepository.updateCheckUpkeepUserByIds(ids);

    }

    /**
     * 根据检保项目编号以及人员类型获取指定类型人员信息
     *
     * @param checkUpkeepId 检保信息编号
     * @param personType    人员类型
     * @return
     */
    @Override
    public List<CheckUpkeepUserVO> getCheckUpkeepUserVOList(String checkUpkeepId, String personType) {

        List<CheckUpkeepUserDO> checkUpkeepUserDOS = checkUpkeepUserRepository.getByCheckUpkeepIdAndStatus(checkUpkeepId, personType);
        return CheckUpkeepUserConvert.INSTANCE.toVOList(checkUpkeepUserDOS);
    }

    /**
     * 获取所有审核人员信息
     *
     * @return
     */
    @Override
    public List<CheckUpkeepUserVO> getRecheckUserVOList() {
        List<CheckUpkeepUserDO> recheckUserVOList = checkUpkeepUserRepository.getRecheckUserList(CheckUpkeepUserTypeEnum.RECHECK.getType());
        return CheckUpkeepUserConvert.INSTANCE.toVOList(recheckUserVOList);
    }

    @Override
    public void deleteDataByCheckUpkeepId(String checkUpkeepId) {
        CheckUpkeepUserDO checkUpkeepUserDO = new CheckUpkeepUserDO();
        checkUpkeepUserDO.setCheckUpkeepId(checkUpkeepId);
        checkUpkeepUserDO.setIsDeleted(DeletedEnum.YES.getCode());
        checkUpkeepUserRepository.updateDataByCheckUpkeepId(checkUpkeepUserDO);
    }


}
